const {Controller}=require("egg")

const {createId}=require("../utils/utils")

class viewsController extends Controller{

    // 获取;
    async index(){
        const {ctx}=this
        const result=await ctx.service.authView.index()
        if(result.length){
            ctx.body={
                code:"1001",
                msg:"获取数据成功",
                result
            }
        }else{
            ctx.body={
                code:"1002",
                msg:"获取数据失败"
            }
        }
    }

    // 查看
    async show(){
        const {ctx}=this
        const {id}=ctx.params
        const result=await ctx.service.authView.show(id)
        if(result.length){
            ctx.body={
                code:"1001",
                msg:"查看成功",
                result
            }
        }else{
            ctx.body={
                code:"1002",
                msg:"查看失败"
            }
        }
    }

    // 编辑
    async update(){
        const {ctx}=this
        const {id}=ctx.params
        const {views_text,views_url}=ctx.request.body
        const result=await ctx.service.authView.update(id,views_text,views_url)
        if(result.affectedRows){
            ctx.body={
                code:"1001",
                msg:"编辑成功"
            }
        }else{
            ctx.body={
                code:"1002",
                msg:"编辑失败"
            }
        }
    }

    // 删除
    async destroy(){
        const {ctx}=this
        const {id}=ctx.params
        const result=await ctx.service.authView.destroy(id)
        if(result.affectedRows){
            ctx.body={
                code:"1001",
                msg:"删除成功"
            }
        }else{
            ctx.body={
                code:"1002",
                msg:"删除失败"
            }
        }
    }

    // 增加
    async create(){
        const {ctx}=this
        const {views_text,views_url}=ctx.request.body
        const id=createId(views_text)
        const result=await ctx.service.authView.create(id,views_text,views_url)
        if(result.affectedRows){
            ctx.body={
                code:"1001",
                msg:"增加成功"
            }
        }else{
            ctx.body={
                code:"1002",
                msg:"增加失败"
            }
        }
    }
}

module.exports=viewsController